﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TestTableDataExtractionComponent:WorkflowComponentBase
    {
        #region props
        private TestTableDataExtractionInstruction _Instruction;
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction=(TestTableDataExtractionInstruction) value; }
        }

        private TestTableDataExtractionOutput _Output;
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output=(TestTableDataExtractionOutput)value; }
        }

        /// <summary></summary>
        public string CorrectResultConnStr
        {
            get
            {
                return this._Instruction.CorrectResultConnStr;
            }
            set
            {
                this._Instruction.CorrectResultConnStr = value;
            }
        }

        /// <summary></summary>
        public string CorrectResultSelectSql
        {
            get
            {
                return this._Instruction.CorrectResultSelectSql;
            }
            set
            {
                this._Instruction.CorrectResultSelectSql = value;
            }
        }

        /// <summary></summary>
        public string CorrectResultDocIDField
        {
            get
            {
                return this._Instruction.CorrectResultDocIDField;
            }
            set
            {
                this._Instruction.CorrectResultDocIDField = value;
            }
        }

        /// <summary></summary>
        public string CorrectResultPageNumberField
        {
            get
            {
                return this._Instruction.CorrectResultPageNumberField;
            }
            set
            {
                this._Instruction.CorrectResultPageNumberField = value;
            }
        }

        /// <summary></summary>
        public string OurDocListResultFilePath
        {
            get
            {
                return this._Instruction.OurDocListResultFilePath;
            }
            set
            {
                this._Instruction.OurDocListResultFilePath = value;
            }
        }

        /// <summary></summary>
        public string OurTableScanResultFilePath
        {
            get
            {
                return this._Instruction.OurTableScanResultFilePath;
            }
            set
            {
                this._Instruction.OurTableScanResultFilePath = value;
            }
        }

        /// <summary></summary>
        public string OurTableLayoutResultFilePath
        {
            get
            {
                return this._Instruction.OurTableLayoutResultFilePath;
            }
            set
            {
                this._Instruction.OurTableLayoutResultFilePath = value;
            }
        }

        /// <summary></summary>
        public string OurTableExtractResultFolderPath
        {
            get
            {
                return this._Instruction.OurTableExtractResultFolderPath;
            }
            set
            {
                this._Instruction.OurTableExtractResultFolderPath = value;
            }
        }

        /// <summary></summary>
        public string DocListPerformanceResultFilePath
        {
            get
            {
                return this._Instruction.DocListPerformanceResultFilePath;
            }
            set
            {
                this._Instruction.DocListPerformanceResultFilePath = value;
            }
        }

        /// <summary></summary>
        public string TableScanPerformanceResultFilePath
        {
            get
            {
                return this._Instruction.TableScanPerformanceResultFilePath;
            }
            set
            {
                this._Instruction.TableScanPerformanceResultFilePath = value;
            }
        }

        /// <summary></summary>
        public string TableLayoutPerformanceResultFilePath
        {
            get
            {
                return this._Instruction.TableLayoutPerformanceResultFilePath;
            }
            set
            {
                this._Instruction.TableLayoutPerformanceResultFilePath = value;
            }
        }

        /// <summary></summary>
        public string TableExtractPerformanceResultFilePath
        {
            get
            {
                return this._Instruction.TableExtractPerformanceResultFilePath;
            }
            set
            {
                this._Instruction.TableExtractPerformanceResultFilePath = value;
            }
        }

        /// <summary></summary>
        public Dictionary<string, string> SrcTgtFieldMappings
        {
            get
            {
                return this._Instruction.SrcTgtFieldMappings;
            }
            set
            {
                this._Instruction.SrcTgtFieldMappings = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public TestTableDataExtractionComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Enumeration.ToString();
            this.ComponentName = "Tester";
            this.Description = "Test table extraction performances";
            this.ComponentIcon = Resources.FXSRES_5034;
            this.ComponentImage = Resources.Journal2;

            this._Instruction =
                new TestTableDataExtractionInstruction(
                    this.ComponentID, Guid.Empty,
                    string.Empty, string.Empty, string.Empty, string.Empty, 
                    string.Empty, string.Empty, string.Empty, string.Empty, 
                    string.Empty, string.Empty, string.Empty, string.Empty,
                    new Dictionary<string, string>());
            this._Output =
                new TestTableDataExtractionOutput(
                    this.ComponentID, Guid.Empty,
                    0, 0, 0, 0, string.Empty,
                    0, 0, 0, string.Empty,
                    0, 0, 0, string.Empty,
                    0, 0, 0, string.Empty);
        }
        #endregion

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TestTableDataExtractionComponent component = base.Instantiate(xNode) as TestTableDataExtractionComponent;

                component.CorrectResultConnStr = XmlDataUtil.GetAttributeValue(xNode, "CorrectResultConnectionString",
                                                                                 "");
                component.CorrectResultDocIDField = XmlDataUtil.GetAttributeValue(xNode, "CorrectResultDocIDField", "");
                component.CorrectResultSelectSql = XmlDataUtil.GetAttributeValue(xNode, "CorrectResultSelectSql", "");
                component.CorrectResultPageNumberField = XmlDataUtil.GetAttributeValue(xNode,
                                                                                         "CorrectResultPageNumberField",
                                                                                         "");

                component.OurDocListResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "OurDocListResultFilePath",
                                                                                     "");
                component.OurTableScanResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                       "OurTableScanResultFilePath", "");
                component.OurTableLayoutResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                         "OurTableLayoutResultFilePath",
                                                                                         "");
                component.OurTableExtractResultFolderPath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                            "OurTableExtractResultFolderPath",
                                                                                            "");

                component.DocListPerformanceResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                             "DocListPerformanceResultFilePath",
                                                                                             "");
                component.TableScanPerformanceResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                               "TableScanPerformanceResultFilePath",
                                                                                               "");
                component.TableLayoutPerformanceResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                                 "TableLayoutPerformanceResultFilePath",
                                                                                                 "");
                component.TableExtractPerformanceResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                                  "TableExtractPerformanceResultFilePath",
                                                                                                  "");

                XmlNodeList mappingNodes = xNode.SelectNodes("FieldMappings/FieldMapping");
                if (mappingNodes != null && mappingNodes.Count > 0)
                {
                    component.SrcTgtFieldMappings = new Dictionary<string, string>();
                    foreach (XmlNode mappingNode in mappingNodes)
                    {
                        string srcFieldName = XmlDataUtil.GetAttributeValue(mappingNode, "FieldNameInFile", "");
                        if (!component.SrcTgtFieldMappings.ContainsKey(srcFieldName))
                        {
                            string tgtFieldName = XmlDataUtil.GetAttributeValue(mappingNode, "FieldNameInTable", "");
                            component.SrcTgtFieldMappings.Add(srcFieldName, tgtFieldName);
                        }
                    }
                }

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate email: invalid type specified in xml node");
            }
        }


        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CorrectResultConnectionString", this.CorrectResultConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CorrectResultSelectSql", this.CorrectResultSelectSql);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CorrectResultPageNumberField",
                                        this.CorrectResultPageNumberField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CorrectResultDocIDField", this.CorrectResultDocIDField);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OurDocListResultFilePath", this.OurDocListResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OurTableScanResultFilePath", this.OurTableScanResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OurTableLayoutResultFilePath",
                                        this.OurTableLayoutResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OurTableExtractResultFolderPath",
                                        this.OurTableExtractResultFolderPath);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocListPerformanceResultFilePath",
                                        this.DocListPerformanceResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableScanPerformanceResultFilePath",
                                        this.TableScanPerformanceResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableLayoutPerformanceResultFilePath",
                                        this.TableLayoutPerformanceResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableExtractPerformanceResultFilePath",
                                        this.TableExtractPerformanceResultFilePath);

            if (this.SrcTgtFieldMappings != null && this.SrcTgtFieldMappings.Count > 0)
            {
                XmlNode mappingRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "FieldMappings");
                foreach (string srcField in this.SrcTgtFieldMappings.Keys)
                {
                    string tgtFieldName = this.SrcTgtFieldMappings[srcField];
                    XmlNode mappingNode = XmlDataUtil.AddElement(ref xDoc, mappingRootNode, "FieldMapping");
                    XmlDataUtil.UpdateAttribute(ref xDoc, mappingNode, "FieldNameInFile", srcField);
                    XmlDataUtil.UpdateAttribute(ref xDoc, mappingNode, "FieldNameInTable", tgtFieldName);
                }
            }

            return xNode;
        }
    }
}
